package com.cy.letcode.矩阵相关.螺旋矩阵打印;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 给定一个包含 m x n 个元素的矩阵（m 行, n 列），请按照顺时针螺旋顺序，返回矩阵中的所有元素。
 * <p>
 * 示例 1:
 * <p>
 * 输入: 3*3  2 2*2 1 4*4 2 5*5 3
 * [
 * [ 1, 2, 3 ],
 * [ 4, 5, 6 ],
 * [ 7, 8, 9 ]
 * ]
 * 输出: [1,2,3,6,9,8,7,4,5]
 * 示例 2:
 * <p>
 * 输入:
 * [
 * [1, 2, 3, 4],
 * [5, 6, 7, 8],
 * [9,10,11,12]
 * ]
 * 输出: [1,2,3,4,8,12,11,10,9,5,6,7]
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/spiral-matrix
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
class Solution {
    public static void main(String[] args) {
        int[][] matrix = {
                {1, 2, 3},
                {1, 2, 3},
                {5, 6, 7}
        };

        List<Integer> integers = new Solution().spiralOrder(matrix);
        integers.forEach(e -> System.out.print(e + ","));
    }


    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> lists = new ArrayList<>();
        int height = matrix.length;
        int width = matrix[0].length;
        int nums = height * width;
        int level = 0;
        while (lists.size() < nums) {
            for (int i = level; i < width - level; i++) {
                lists.add(matrix[level][i]);
            }
            for (int i = level + 1; i < height - level; i++) {
                lists.add(matrix[i][width - 1 - level]);
            }
            for (int i = width - 2 - level; i >= level; i--) {
                lists.add(matrix[height - 1 - level][i]);
            }
            for (int i = height - 2 - level; i > level; i--) {
                lists.add(matrix[i][level]);
            }
            level++;
        }
        return lists;

    }


}